package com.zzyl.controller.manager;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.zzyl.base.ResponseResult;
import com.zzyl.file.FileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private FileStorageService fileStorageService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ResponseResult<String> upload(MultipartFile file) throws IOException {
        try {
            // 通过文件的输入流读取图像并将其转换为BufferedImage对象
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            
        } catch (Exception e) {
            throw new RuntimeException("上传图片失败");
        }
        String filename = file.getOriginalFilename();//获取到文件名。例:a.jpg
        
        //获取文件后缀名
        String suffix = FileUtil.getSuffix(filename);
        
        String  uuid = UUID.randomUUID().toString();
        
        String objectName = uuid + "." + suffix;
        
        String store = fileStorageService.store(objectName, file.getInputStream());
        
        return ResponseResult.success(store);
    }
    
}
